package com.ikangtai.shecare.curve.mpchart;


import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.common.al.DayUnitDSOutput;
import com.ikangtai.shecare.common.util.AppLog;
import com.ikangtai.shecare.common.util.DateUtil;

import java.util.ArrayList;
import java.util.List;

public class DangerPeriod {
    /**
     * BBTDangerPeriodStart 里记录的是 dayUnitDSOutputsList 里所有dayUnitDSOutput为危险期开始的下标位置position,
     * BBTDangerPeriodEnd 里记录的是 dayUnitDSOutputsList 里所有dayUnitDSOutput为危险期结束的下标位置position.
     */
    private static List<Integer> BBTDangerPeriodStart = new ArrayList<>();
    private static List<Integer> BBTDangerPeriodEnd = new ArrayList<>();

    private static boolean inited = false;

    public static void init(){
        initBBTDangerPeriodStartEndDayList();
        initBBTLineChartxValue2PeriodMap();
        inited = true;
    }

    private static void initBBTDangerPeriodStartEndDayList(){
        BBTDangerPeriodStart.clear();
        BBTDangerPeriodEnd.clear();

        for(int i = 0; i < App.dayUnitDSOutputsList.size(); i++){
            AppLog.i("BBTLineChart dayUnitDSOutputsList date = " + DateUtil.getSimpleCircleCalendarTopDate(App.dayUnitDSOutputsList.get(i).date));
            AppLog.i("BBTLineChart dayUnitDSOutputsList period = " + App.dayUnitDSOutputsList.get(i).getPeriod(App.status));

            float dangerPeriodXAxisValue = BBTLineUtil.getXAxisValue(App.dayUnitDSOutputsList.get(i).date);

            if(isDangerPeriodStart(i) && BBTLineUtil.isValidXAxisValue(dangerPeriodXAxisValue) || isFirstXAxisValueDangerPeriod(dangerPeriodXAxisValue, i))
                BBTDangerPeriodStart.add(i);

            if(isDangerPeriodEnd(i) && BBTLineUtil.isValidXAxisValue(dangerPeriodXAxisValue) || isFinalXAxisValueDangerPeriod(dangerPeriodXAxisValue, i))
                BBTDangerPeriodEnd.add(i);
        }
    }

    private static boolean isDangerPeriodStart(int position){
        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        if(position == 0 && period.equals(DayUnitDSOutput.PERIOD_WXQ)) return true;
        if(position == 0 && !period.equals(DayUnitDSOutput.PERIOD_WXQ)) return false;

        DayUnitDSOutput previousDayUnitDSOutput = App.dayUnitDSOutputsList.get(position - 1);
        String previousDayPeriod = previousDayUnitDSOutput.getPeriod(App.status);

        if(period.equals(DayUnitDSOutput.PERIOD_WXQ) &&
                !previousDayPeriod.equals(DayUnitDSOutput.PERIOD_WXQ))
            return true;
        else
            return false;
    }

    private static boolean isDangerPeriodEnd(int position){

        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        if(position == App.dayUnitDSOutputsList.size() - 1 && period.equals(DayUnitDSOutput.PERIOD_WXQ)) return true;
        if(position == App.dayUnitDSOutputsList.size() - 1 && !period.equals(DayUnitDSOutput.PERIOD_WXQ)) return false;

        DayUnitDSOutput NextDayUnitDSOutput = App.dayUnitDSOutputsList.get(position + 1);
        String nextDayPeriod = NextDayUnitDSOutput.getPeriod(App.status);

        if(period.equals(DayUnitDSOutput.PERIOD_WXQ) &&
                !nextDayPeriod.equals(DayUnitDSOutput.PERIOD_WXQ))
            return true;
        else
            return false;
    }

    /**
     * 刚好曲线X轴的第一天为处于危险期，但是在 dayUnitDSOutputsList 中是危险期的中间位置，非起始位置
     * @param dangerPeriodXAxisValue
     * @param position
     * @return
     */
    private static boolean isFirstXAxisValueDangerPeriod(float dangerPeriodXAxisValue, int position){
        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        return dangerPeriodXAxisValue == BBTLineChart.MIN_XAXIS_DAY && period.equals(DayUnitDSOutput.PERIOD_WXQ);
    }

    /**
     * 刚好曲线X轴的最后一天为处于危险期，但是在 dayUnitDSOutputsList 中是危险期的中间位置，非末位置
     * @param dangerPeriodXAxisValue
     * @param position
     * @return
     */
    private static boolean isFinalXAxisValueDangerPeriod(float dangerPeriodXAxisValue, int position){
        DayUnitDSOutput dayUnitDSOutput = App.dayUnitDSOutputsList.get(position);
        String period = dayUnitDSOutput.getPeriod(App.status);

        return dangerPeriodXAxisValue == BBTLineChart.MAX_XAXIS_DAY && period.equals(DayUnitDSOutput.PERIOD_WXQ);
    }

    public static void initBBTLineChartxValue2PeriodMap(){
        for(int i = 0; i < BBTDangerPeriodStart.size(); i++){
            int dangerPeriodStartPoisition = BBTDangerPeriodStart.get(i);
            int dangerPeriodEndPoisition = BBTDangerPeriodEnd.get(i);

            AppLog.i("BBTLineChart Danger background, i = " + i + ", dangerPeriodStartPoisition = " + dangerPeriodStartPoisition + ", dangerPeriodEndPoisition = " + dangerPeriodEndPoisition);

            initDangerXValue2PeriodMap(dangerPeriodStartPoisition, dangerPeriodEndPoisition);
        }
    }

    private static void initDangerXValue2PeriodMap(int dangerPeriodStartPoisition, int dangerPeriodEndPoisition){
        for(int dangerPeriodPoistion = dangerPeriodStartPoisition; dangerPeriodPoistion <= dangerPeriodEndPoisition; dangerPeriodPoistion ++){
            DayUnitDSOutput dangerPeriodDayUnitDSOutput = App.dayUnitDSOutputsList.get(dangerPeriodPoistion);
            float dangerPeriodXAxisValue = BBTLineUtil.getXAxisValue(dangerPeriodDayUnitDSOutput.date);

            AppLog.i("BBTLineChart dangerPeriodXAxisValue = " + DateUtil.getSimpleCircleCalendarTopDate(dangerPeriodDayUnitDSOutput.date) + ", x = " + BBTLineUtil.getXAxisValue(dangerPeriodDayUnitDSOutput.date));

            BBTLineChart.bbtLineChartxValue2PeriodMap.put(dangerPeriodXAxisValue, DayUnitDSOutput.PERIOD_WXQ);
        }
    }

    public static boolean isInDangerPeriod(long daySeconds){
//        boolean inDangerPeriod = false;
//
//        for(int i = 0; i < BBTDangerPeriodStart.size(); i++) {
//            int dangerPeriodStartPoisition = BBTDangerPeriodStart.get(i);
//            DayUnitDSOutput dangerPeriodStartDayUnitDSOutput = App.dayUnitDSOutputsList.get(dangerPeriodStartPoisition);
//
//            int dangerPeriodEndPoisition = BBTDangerPeriodEnd.get(i);
//            DayUnitDSOutput dangerPeriodEndDayUnitDSOutput = App.dayUnitDSOutputsList.get(dangerPeriodEndPoisition);
//
//            if(daySeconds >= dangerPeriodStartDayUnitDSOutput.date - 12 * 3600 && daySeconds <= dangerPeriodEndDayUnitDSOutput.date + 12 * 3600){
//                inDangerPeriod = true;
//                break;
//            }
//        }
//
//        return inDangerPeriod;

        float xAxisValue = BBTLineUtil.getXAxisValue(daySeconds);
        return BBTLineChart.bbtLineChartxValue2PeriodMap.get(xAxisValue) == DayUnitDSOutput.PERIOD_WXQ;
    }
    
    public static List<Integer> getBBTDangerPeriodStartList(){
        if(!inited)
            init();

        return BBTDangerPeriodStart;
    }

    public static List<Integer> getBBTDangerPeriodEndList(){
        if(!inited)
            init();

        return BBTDangerPeriodEnd;
    }
}
